package com.ygqh.baby.controller.mobile;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ygqh.baby.annotation.YgController;
import com.ygqh.baby.ao.CheckStatus;
import com.ygqh.baby.ao.CommentType;
import com.ygqh.baby.ao.ResponseEntity;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.model.CommentCountModel;
import com.ygqh.baby.model.YgIndexModel;
import com.ygqh.baby.model.YgNotSaleHeadModel;
import com.ygqh.baby.model.YgPublishCommentModel;
import com.ygqh.baby.model.YgSimilarityPublishModel;
import com.ygqh.baby.po.YgPublish;
import com.ygqh.baby.po.YgPublishAccusation;
import com.ygqh.baby.po.YgPublishComment;
import com.ygqh.baby.po.YgPublishDetail;
import com.ygqh.baby.po.YgPublishTag;
import com.ygqh.baby.po.YgUser;
import com.ygqh.baby.service.YgPublishAccusationService;
import com.ygqh.baby.service.YgPublishCommentService;
import com.ygqh.baby.service.YgPublishService;
import com.ygqh.baby.service.YgPublishTagService;
import com.ygqh.baby.utils.SessionUtil;

/**
 * 晒单接口
 * 
 * @author ZhangSai
 *
 */
@Controller
@RequestMapping("/app/publish")
public class YgPublishController extends BaseController {

	@Autowired
	private YgPublishService ygPublishService;

	@Autowired
	private YgPublishCommentService ygPublishCommentService;
	@Autowired
	private YgPublishAccusationService ygPublishAccusationService;
	@Autowired
	private YgPublishTagService publishTagService;

	/**
	 * 首页接口
	 * 
	 * @param callback
	 * @param queryInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/search4Index")
	@ResponseBody
	public ResponseEntity search4Index(QueryInfo queryInfo) throws Exception {
		ResultSet<YgIndexModel> resultSet = ygPublishService.searchModel4Index(queryInfo);
		return ResponseEntity.success(resultSet.getResult());
	}

	/**
	 * TA的晒单
	 * 
	 * @param callback
	 * @param queryInfo
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/search4User")
	@ResponseBody
	public ResponseEntity search4User(QueryInfo queryInfo, @RequestParam(required = true) Long userId) throws Exception {
		YgUser currentUser = SessionUtil.getCurrentUser();
		ResultSet<YgIndexModel> resultSet = null;
		if (currentUser == null) {
			resultSet = ygPublishService.searchModel4User(queryInfo, userId);
		} else {
			if (currentUser.getId().equals(userId)) {
				resultSet = ygPublishService.searchModel4Me(queryInfo, userId);
			} else {
				resultSet = ygPublishService.searchModel4User(queryInfo, userId);
			}
		}
		return ResponseEntity.success(resultSet.getResult());
	}

	/**
	 * TA喜欢的晒单
	 * 
	 * @param callback
	 * @param queryInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/searchModel4Praise")
	@ResponseBody
	public ResponseEntity searchModel4Praise(QueryInfo queryInfo) throws Exception {
		ResultSet<YgIndexModel> resultSet = ygPublishService.searchModel4Praise(queryInfo, SessionUtil.getCurrentUser().getId());
		return ResponseEntity.success(resultSet.getResult());
	}

	/**
	 * 点赞
	 * 
	 * @param callback
	 * @param publishId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/pushNice")
	@ResponseBody
	public ResponseEntity pushNice(@RequestParam(required = true) Long publishId, @RequestParam(required = true) Long receiverId) throws Exception {
		YgPublishComment comment = new YgPublishComment();
		YgUser user = SessionUtil.getCurrentUser();
		comment.setUserId(user.getId());
		comment.setPublishId(publishId);
		comment.setCreateTime(new Date());
		comment.setPublishType(CommentType.Praise);
		comment.setAuditStatus(CheckStatus.Pass);
		ygPublishCommentService.pushNice(comment, receiverId);
		return ResponseEntity.success(null);
	}

	/**
	 * 根据标签查晒单
	 * 
	 * @param callback
	 * @param queryInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/selectModel4Tag")
	@ResponseBody
	public ResponseEntity selectModel4Tag(QueryInfo queryInfo, @RequestParam(required = true) String tag) throws Exception {
		ResultSet<YgIndexModel> resultSet = ygPublishService.selectModel4Tag(queryInfo, tag);
		return ResponseEntity.success(resultSet.getResult());
	}

	@Deprecated
	@RequestMapping(value = "/getNiceCount")
	@ResponseBody
	public ResponseEntity getNiceCount(@RequestParam(required = true) Long[] publishIds) throws Exception {

		if (publishIds == null)
			ResponseEntity.error(null);

		List<CommentCountModel> result = ygPublishCommentService.findCountByPublishIds(CommentType.Praise, publishIds);
		return ResponseEntity.success(result);
	}

	@Deprecated
	@RequestMapping(value = "/getCommentCount")
	@ResponseBody
	public ResponseEntity getCommentCount(@RequestParam(required = true) Long[] publishIds) throws Exception {

		if (publishIds == null)
			ResponseEntity.error(null);

		List<CommentCountModel> result = ygPublishCommentService.findCountByPublishIds(CommentType.Comment, publishIds);
		return ResponseEntity.success(result);
	}

	/**
	 * 获取点赞数以及评论数
	 * 
	 * @param callback
	 * @param publishIds
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCommentAndNiceCount")
	@ResponseBody
	public ResponseEntity getCommentAndNiceCount(@RequestParam(required = true) Long[] publishIds) throws Exception {

		if (publishIds == null || publishIds.length == 0)
			return ResponseEntity.success(null);

		List<CommentCountModel> commentResult = ygPublishCommentService.findCountByPublishIds(CommentType.Comment, publishIds);
		List<CommentCountModel> praiseResult = ygPublishCommentService.findCountByPublishIds(CommentType.Praise, publishIds);
		if (SessionUtil.getCurrentUser() != null) {
			Long[] userPublishIds = ygPublishCommentService.findPublishIdsByUser(SessionUtil.getCurrentUser().getId());
			List<Long> userPubllishIdList = Arrays.asList(userPublishIds);
			for (CommentCountModel model : praiseResult) {
				if (userPubllishIdList.contains(model.getPublishId())) {
					model.setCurrentUserIsPraise(true);
				}
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commentResult", commentResult);
		map.put("praiseResult", praiseResult);
		return ResponseEntity.success(map);
	}

	/**
	 * 晒单评论详情页-主信息
	 * 
	 * @param callback
	 * @param publishId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findDetailModel")
	@ResponseBody
	public ResponseEntity findDetailModel(@RequestParam(required = true) Long publishId) throws Exception {
		YgIndexModel model = ygPublishService.findDetailModel(publishId);
		return ResponseEntity.success(model);
	}

	/**
	 * 晒单评论详情页-评论
	 * 
	 * @param callback
	 * @param publishId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findCommentByPublishId")
	@ResponseBody
	public ResponseEntity findCommentByPublishId(@RequestParam(required = true) Long publishId) throws Exception {
		List<YgPublishCommentModel> comments = ygPublishCommentService.findCommentByPublishId(publishId);
		return ResponseEntity.success(comments);
	}

	/**
	 * 非售商品-头部
	 * 
	 * @param callback
	 * @param pdtId
	 *            晒单图上的标签id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/notSaleHead")
	@ResponseBody
	public ResponseEntity notSaleHead( @RequestParam(required = true) Long pdtId) throws Exception {
		YgNotSaleHeadModel model = ygPublishService.findNotSaleHeadModel(pdtId);
		return ResponseEntity.success(model);
	}

	/**
	 * 相似晒单
	 * 
	 * @param callback
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/similarityPublish")
	@YgController
	@ResponseBody
	public ResponseEntity similarityPublish(@RequestParam(required = true) Long productId, @RequestParam(required = true) Long groupId) throws Exception {

		List<YgSimilarityPublishModel> result = ygPublishService.findSimilarityPublishModel(productId, groupId, 67L);
		return ResponseEntity.success(result);

	}

	/**
	 * 非售-相似晒单
	 * 
	 * @param callback
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/similarityPublish4NotSale")
	@ResponseBody
	public ResponseEntity similarityPublish4NotSale(@RequestParam(required = true) String brandName, @RequestParam(required = true) String categoryId)
			throws Exception {

		List<YgSimilarityPublishModel> result = ygPublishService.similarityPublish4NotSale(brandName, categoryId, 67L);
		return ResponseEntity.success(result);
	}

	/**
	 * 发布晒单
	 * 
	 * @param callback
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/publish")
	@ResponseBody
	public ResponseEntity publish(@RequestParam(required = true) String tags, @RequestParam(required = true) String publishDesc,
			@RequestParam(required = true) String publishDetail) throws Exception {

		YgPublish publish = new YgPublish();
		publish.setTags(tags);
		publish.setPublishDesc(publishDesc);
		publish.setCheckStatus(CheckStatus.Waiting.name());
		publish.setSortNum(9999L);
		publish.setUserId(SessionUtil.getCurrentUser().getId());
		publish.setCreateTime(new Date());

		List<YgPublishDetail> publishDetailList = JSON.parseObject(publishDetail, new TypeReference<List<YgPublishDetail>>() {
		});
		int publishId = ygPublishService.save(publish, publishDetailList);
		Map<String, Object> map = new HashMap<>();
		map.put("publishId", publishId);
		return ResponseEntity.success(map);
	}

	/**
	 * 举报晒单
	 * 
	 * @param callback
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/publishReport")
	@ResponseBody
	public ResponseEntity publishReport(String remark, @RequestParam(required = true) Long publishId)
			throws Exception {
		YgUser user = SessionUtil.getCurrentUser();
		YgPublishAccusation model = new YgPublishAccusation();
		model.setUserId(user.getId());
		model.setPublishId(publishId);
		model.setRemark(remark);
		model.setCreateTime(new Date());
		int row = ygPublishAccusationService.save(model);
		return ResponseEntity.success(row);
	}

	/**
	 * 晒单评论添加
	 * 
	 * @param callback
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/publishComment")
	@ResponseBody
	public ResponseEntity publishComment(@RequestParam(required = true) Long receiverId, @RequestParam(required = true) String content,
			@RequestParam(required = true) Long publishId) throws Exception {
		int r = ygPublishService.publishComment(publishId, content, SessionUtil.getCurrentUser().getId(), receiverId);
		return ResponseEntity.success(r);
	}

	/********************************** 以下是新晒单相关接口 ***********************************************/

	/**
	 * new首页接口
	 * 
	 * @param callback
	 * @param queryInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/newSearch4Index")
	@ResponseBody
	public ResponseEntity newSearch4Index(QueryInfo queryInfo) throws Exception {
		ResultSet<YgIndexModel> resultSet = ygPublishService.newSearchModel4Index(queryInfo);
		return ResponseEntity.success(resultSet.getResult());
	}

	/**
	 * newTA的晒单
	 * 
	 * @param callback
	 * @param queryInfo
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/newSearch4User")
	@ResponseBody
	public ResponseEntity newSearch4User(QueryInfo queryInfo, @RequestParam(required = true) Long userId) throws Exception {

		YgUser currentUser = SessionUtil.getCurrentUser();
		ResultSet<YgIndexModel> resultSet = null;
		if (currentUser == null) {
			resultSet = ygPublishService.newSearchModel4User(queryInfo, userId);
		} else {
			if (currentUser.getId().equals(userId)) {
				resultSet = ygPublishService.searchModel4Me(queryInfo, userId);
			} else {
				resultSet = ygPublishService.newSearchModel4User(queryInfo, userId);
			}
		}
		return ResponseEntity.success(resultSet.getResult());
	}

	/**
	 * newTATA喜欢的晒单
	 * 
	 * @param callback
	 * @param queryInfo
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/newSearchModel4Praise")
	@ResponseBody
	public ResponseEntity newSearchModel4Praise(@RequestParam(required = true) Long userId, QueryInfo queryInfo) throws Exception {
		ResultSet<YgIndexModel> resultSet = ygPublishService.newSearchModel4Praise(queryInfo, userId);
		return ResponseEntity.success(resultSet.getResult());
	}

	/**
	 * newTA根据标签查晒单
	 * 
	 * @param callback
	 * @param queryInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/newSelectModel4Tag")
	@ResponseBody
	public ResponseEntity newSelectModel4Tag(QueryInfo queryInfo, @RequestParam(required = true) String tag) throws Exception {
		ResultSet<YgIndexModel> resultSet = ygPublishService.newSelectModel4Tag(queryInfo, tag);
		return ResponseEntity.success(resultSet.getResult());
	}

	/**
	 * 晒单标签
	 *
	 * @param callback
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/findPublishTag")
	@ResponseBody
	public ResponseEntity findPublishTag() throws Exception {
		List<YgPublishTag> list = publishTagService.find();
		return ResponseEntity.success(list);
	}

	/**
	 * 删除晒单
	 * 
	 * @param callback
	 * @param publishId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/deletePublish")
	@ResponseBody
	public ResponseEntity deletePublish(@RequestParam(required = true) Long publishId) throws Exception {
		ygPublishService.deleteSoft(publishId);
		return ResponseEntity.success(null);
	}
}
